package com.siqi.hr.system.custom.controller;

import com.siqi.hr.common.generator.IdGenerator;
import com.siqi.hr.system.common.enums.OperationEnum;
import com.siqi.hr.system.custom.entity.EntityOperation;
import com.siqi.hr.system.custom.entity.EntityRecord;
import com.siqi.hr.system.custom.filter.EntityOperationFilter;
import com.siqi.hr.system.custom.service.EntityOperationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 实体权限操作控制器(主要处理操作按钮的增删改查)
 * Created by Administrator on 2016/8/22.
 * @author xiaogq
 * @since JDK1.8.0_12
 */
@Controller
@RequestMapping("EntityOperation")
public class EntityOperationController {

    @Autowired
    private EntityOperationService entityOperationService;

    /**
     * 初始化实体操作列表页面(xiaogq)
     * @param request
     * @return
     */
    @RequestMapping(value = "initOperationList", method = RequestMethod.GET)
    public ModelAndView initEntity(HttpServletRequest request) {
        ModelAndView mav = new ModelAndView();
        EntityOperation operation = new EntityOperation("新建操作","addEntityOperation","EntityOperation/initAddOperation.do","image/common/operation/oper_ADD.png", OperationEnum.showList.name(),new EntityRecord(0));
        List<EntityOperation> listButtons = new ArrayList<EntityOperation>();
        listButtons.add(operation);
        //得到系统设置对象管理列头
        Map<String, Object> columnMap = entityOperationService.queryEntityOperationListColumns();
        mav.addObject("listButtons",listButtons);
        mav.addObject("columns",columnMap.get("columns"));
        mav.addObject("entity", new EntityRecord(0,"实体操作"));
        mav.setViewName("system/custom/operation/operation_list");
        return mav;
    }

    /**
     * 查询所有的系统对象或者自定义对象(xiaogq)
     * @param request
     * @param operationFilter      实体操作搜索条件对象信息
     * @return
     * @throws IOException
     */
    @RequestMapping(value = "queryEntityOperationList")
    @ResponseBody
    public Map<String,Object> queryEntityList(HttpServletRequest request, EntityOperationFilter operationFilter) throws Exception {
        //得到系统设置操作管理列表数据
        Map<String,Object> resultMap = entityOperationService.queryAllOperation(operationFilter);
        return resultMap;
    }

    /**
     * 添加操作对象
     * @author         xiaoxm
     * @return
     */
    @RequestMapping(value = "initAddOperation", method = RequestMethod.GET)
    public ModelAndView initAddOperation() {
        return new ModelAndView("system/custom/operation/operation_add");
    }

    /**
     * 添加操作保存方法
     * @author         xiaoxm
     * @param entityOperation    操作对象
     * @Return ModelAndView
     */
    @RequestMapping(value="operationAdd", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> operationAdd(HttpServletRequest request,EntityOperation entityOperation) throws Exception{
        entityOperation.setId(IdGenerator.createId(0));
        entityOperation.setOperationType(0);
        entityOperation.setSysOperation(false);
        //添加操作时默认是不受权限控制
        entityOperation.setPermission(false);
        entityOperation.setPosition(0);
        Map<String, Object> map = new HashMap<String, Object>();
        int counts = entityOperationService.addEntityOperation(entityOperation);
        if(counts > 0){
            map.put("results","success");
        }else{
            map.put("results","error");
        }
        return map;
    }

    /**
     * 修改操作对象
     * @author         xiaoxm
     * @return
     */
    @RequestMapping(value = "initEditOperation", method = RequestMethod.GET)
    public ModelAndView initEditOperation(String operationId) throws Exception{
        EntityOperation operation = entityOperationService.queryEntityOperationById(operationId);
        ModelAndView mav = new ModelAndView();
        mav.addObject("operation", operation);
        mav.setViewName("system/custom/operation/operation_edit");
        return mav;
    }

    /**
     * 修改操作保存方法
     * @author         xiaoxm
     * @param entityOperation    操作对象
     * @Return ModelAndView
     */
    @RequestMapping(value="operationEdit", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> operationEdit(HttpServletRequest request,EntityOperation entityOperation) throws Exception{
        entityOperation.setSysOperation(false);
        Map<String, Object> map = new HashMap<String, Object>();
        int counts = entityOperationService.updateEntityOperation(entityOperation);
        if(counts > 0){
            map.put("results","success");
        }else{
            map.put("results","error");
        }
        return map;
    }

    /**
     * 查询所有的系统对象或者自定义对象(xiaogq)   deleteEntityOperation
     * @param request
     * @param id             实体操作id
     * @return
     * @throws IOException
     */
    @RequestMapping(value = "deleteEntityOperation")
    @ResponseBody
    public Map<String,Object> deleteEntityOperation(HttpServletRequest request,String id) throws Exception {
        Map<String,Object> resultMap = new HashMap<String,Object>();
        //根据操作id删除系统设置操作管理列表数据
        entityOperationService.deleteEntityOperation(id);
        return resultMap;
    }

}
